package com.test.readData;

import com.test.jdbc.JDBCUtils2;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class readData {

    public static void storeData(String tableName,Map<String, ArrayList<Double>> dataMap) throws SQLException {

        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        queryRunner.update("create table "+tableName+"_data(num int primary key auto_increment," +
                "x1 double ,x2 double ,x3 double ,x4 double ,x5 double ," +
                " x6 double ,x7 double ,x8 double ,x9 double ,x10 double ,x11 double ," +
                "x12 double ,x13 double ,x14 double ,x15 double ,x16 double ,y double);");
        for(int i=0;i<dataMap.get("y").size();i++) {
            queryRunner.update("insert into "+tableName+"_data values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    dataMap.get("x1").get(i),dataMap.get("x2").get(i),dataMap.get("x3").get(i),dataMap.get("x4").get(i),dataMap.get("x5").get(i),
                    dataMap.get("x6").get(i),dataMap.get("x7").get(i),dataMap.get("x8").get(i),dataMap.get("x9").get(i),
                    dataMap.get("x10").get(i),dataMap.get("x11").get(i),dataMap.get("x12").get(i),dataMap.get("x13").get(i),
                    dataMap.get("x14").get(i),dataMap.get("x15").get(i),dataMap.get("x16").get(i),dataMap.get("y").get(i));
        }


    }

    public static Map<String, ArrayList<Double>> dataArray(FileReader fileReader ) throws IOException {

        Map<String, ArrayList<Double>> dataMap = new HashMap<String, ArrayList<Double>>();

        for(int i=0;i<16;i++) {
            dataMap.put("x"+(i+1),new ArrayList<Double>());
        }

        ArrayList<Double> y = new ArrayList<Double>();

        BufferedReader br = new BufferedReader(fileReader);
        String line;
        while ((line=br.readLine())!=null) {
            String[] ss = line.split("\t");
          //  System.out.println(ss[0]);
            for(int i=0;i<ss.length-1;i++){
                dataMap.get("x"+(i+1)).add(Double.parseDouble(ss[i]));
            }

            y.add(Double.parseDouble(ss[ss.length-1]));
        }

        dataMap.put("y",y);


        br.close();

        return dataMap;
    }


}
